package com.example.androidproject.sqlite;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;

public class UserRepository {
    private SQLiteDatabase database;
    private UserDatabaseHelper dbHelper;

    public UserRepository(Context context) {
        dbHelper = new UserDatabaseHelper(context);
        database = dbHelper.getWritableDatabase();
    }

    // 插入用户
    public long insertUser(String username, String password, String gender) {
        ContentValues values = new ContentValues();
        values.put(UserDatabaseHelper.COLUMN_USERNAME, username);
        values.put(UserDatabaseHelper.COLUMN_PASSWORD, password);
        values.put(UserDatabaseHelper.COLUMN_GENDER, gender);

        return database.insert(UserDatabaseHelper.TABLE_USERS, null, values);
    }

    // 验证用户
    public boolean validateUser(String username, String password) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String[] projection = {UserDatabaseHelper.COLUMN_USERNAME};
        String selection = UserDatabaseHelper.COLUMN_USERNAME + " = ? AND " + UserDatabaseHelper.COLUMN_PASSWORD + " = ?";
        String[] selectionArgs = {username, password};

        try (Cursor cursor = db.query(
                UserDatabaseHelper.TABLE_USERS,
                projection,
                selection,
                selectionArgs,
                null,
                null,
                null)) {
            return cursor.getCount() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 检查用户名是否存在
    public boolean isUsernameExists(String username) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String[] projection = {UserDatabaseHelper.COLUMN_USERNAME};
        String selection = UserDatabaseHelper.COLUMN_USERNAME + " = ?";
        String[] selectionArgs = {username};

        try (Cursor cursor = db.query(
                UserDatabaseHelper.TABLE_USERS,
                projection,
                selection,
                selectionArgs,
                null,
                null,
                null)) {
            return cursor.getCount() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 获取所有用户
    public List<User> getAllUsers() {
        List<User> users = new ArrayList<>();
        try (Cursor cursor = database.query(UserDatabaseHelper.TABLE_USERS,
                new String[]{UserDatabaseHelper.COLUMN_ID, UserDatabaseHelper.COLUMN_USERNAME,
                        UserDatabaseHelper.COLUMN_PASSWORD, UserDatabaseHelper.COLUMN_GENDER},
                null, null, null, null, null)) {

            if (cursor.moveToFirst()) {
                @SuppressLint("Range") int idIndex = cursor.getColumnIndex(UserDatabaseHelper.COLUMN_ID);
                @SuppressLint("Range") int usernameIndex = cursor.getColumnIndex(UserDatabaseHelper.COLUMN_USERNAME);
                @SuppressLint("Range") int passwordIndex = cursor.getColumnIndex(UserDatabaseHelper.COLUMN_PASSWORD);
                @SuppressLint("Range") int genderIndex = cursor.getColumnIndex(UserDatabaseHelper.COLUMN_GENDER);

                do {
                    int id = cursor.getInt(idIndex);
                    String username = cursor.getString(usernameIndex);
                    String password = cursor.getString(passwordIndex);
                    String gender = cursor.getString(genderIndex);
                    users.add(new User(id, username, password, gender));
                } while (cursor.moveToNext());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return users;
    }

    // 数据类
    public static class User {
        private int id;
        private String username;
        private String password;
        private String gender;
        // 与字段无关
        private String userIp;

        public User(int id, String username, String password, String gender) {
            this.id = id;
            this.username = username;
            this.password = password;
            this.gender = gender;
        }

        public User() {
        }

        public void setId(int id) {
            this.id = id;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }

        public String getUserIp() {
            return userIp;
        }

        public void setUserIp(String userIp) {
            this.userIp = userIp;
        }

        public int getId() {
            return id;
        }

        public String getUsername() {
            return username;
        }

        public String getPassword() {
            return password;
        }

        public String getGender() {
            return gender;
        }

        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", username='" + username + '\'' +
                    ", password='" + password + '\'' +
                    ", gender='" + gender + '\'' +
                    ", userIp='" + userIp + '\'' +
                    '}';
        }
    }
}
